package com.jgjc.controller;

import com.jgjc.service.FrameMemberService;
import com.jgjc.utils.NumberUtils;
import com.jgjc.utils.ResultResUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@Slf4j
public class FrameMemberController {

    @Resource
    FrameMemberService frameMemberService;

    @GetMapping(value = "/bridge/bridgeSide/bridgeParts/bridgeSite/getList")
    public List<Map<String, Object>> getList(HttpServletRequest request) {
        int bridgeId = Integer.parseInt(request.getParameter("bridgeId"));
        int sideTypeId = Integer.parseInt(request.getParameter("sideTypeId"));
        List<Map<String, Object>> listMap = frameMemberService.getList(bridgeId, sideTypeId);
        return listMap;
    }

    @GetMapping(value = "/bridge/bridgeSide/bridgeParts/getSuperstructureTypeList")
    public List<Map<String, Object>> getSuperstructureTypeList() {
        List<Map<String, Object>> mapList = frameMemberService.getSuperstructureTypeList();
        return mapList;
    }

    @GetMapping(value = "/bridge/bridgeSide/bridgeParts/getMaterialTypeList")
    public List<Map<String, Object>> getMaterialTypeList() {
        List<Map<String, Object>> mapList = frameMemberService.getMaterialTypeList();
        return mapList;
    }

    @GetMapping(value = "/bridge/bridgeSide/bridgeParts/getSupportTypeList")
    public List<Map<String, Object>> getSupportTypeList() {
        List<Map<String, Object>> mapList = frameMemberService.getSupportTypeList();
        return mapList;
    }

    @GetMapping(value = "/bridge/bridgeSide/bridgeParts/getPierTypeList")
    public List<Map<String, Object>> getPierTypeList() {
        List<Map<String, Object>> mapList = frameMemberService.getPierTypeList();
        return mapList;
    }

    @GetMapping(value = "/bridge/bridgeSide/bridgeParts/getAbutmentTypeList")
    public List<Map<String, Object>> getAbutmentTypeList() {
        List<Map<String, Object>> mapList = frameMemberService.getAbutmentTypeList();
        return mapList;
    }

    @GetMapping(value = "/bridge/bridgeSide/bridgeParts/getExpansionJointTypeList")
    public List<Map<String, Object>> getExpansionJointTypeList() {
        List<Map<String, Object>> mapList = frameMemberService.getExpansionJointTypeList();
        return mapList;
    }

    @GetMapping(value = "/bridge/bridgeSide/bridgeParts/getListBySiteNo")
    public List<Map<String, Object>> getListBySiteNo(HttpServletRequest request) {
        int siteId = Integer.parseInt(request.getParameter("siteId") == "" ? "0" : request.getParameter("siteId"));
        List<Map<String, Object>> listMap = frameMemberService.getListBySiteId(siteId);
        return listMap;
    }

    @GetMapping(value = "/bridge/bridgeSide/bridgeParts/bridgeSite/isSpanCombinationValid")
    public Map<String, Object> isSpanCombinationValid(HttpServletRequest request) {
        int bridgeId = Integer.parseInt(request.getParameter("bridgeId"));
        int sideTypeId = Integer.parseInt(request.getParameter("sideTypeId"));
        String bridgeSpanCombination = request.getParameter("bridgeSpanCombination");
        List<Map<String, Object>> mapLst = new ArrayList<>();
        Map<String, Object> mapResult = new HashMap<>();
        String regex = "\\(+\\d+\\*\\d+\\)";
        try {
            String[] arr = bridgeSpanCombination.split("\\+");
            int siteNo = 1;
            for (int i = 0; i < arr.length; i++) {
                if (arr[i].matches(regex)) {
                    if (("(").equals(arr[i].substring(0, 1)) && (")").equals(arr[i].substring(arr[i].length() - 1))) {
                        String a = arr[i].substring(1, arr[i].length() - 1);
                        int ks = Integer.valueOf(a.split("\\*")[0]);
                        String span = a.split("\\*")[1];
                        for (int j = 0; j < ks; j++) {
                            Map<String, Object> map = new HashMap<>();
                            map.put("jointNo", i + 1);
                            map.put("span", span);
                            map.put("bridgeId", bridgeId);
                            map.put("sideTypeId", sideTypeId);
                            map.put("id", null);
                            map.put("siteNo", siteNo);
                            mapLst.add(map);
                            siteNo++;
                        }
                        mapResult.put("content", mapLst);
                        mapResult.put("extra", null);
                        mapResult.put("message", null);
                        mapResult.put("status", "SUCCESS");
                    } else {
                        mapResult.put("content", null);
                        mapResult.put("extra", null);
                        mapResult.put("message", "格式错误，多联格式：(孔数*跨径)+(孔数*跨径)...");
                        mapResult.put("status", "ERROR");
                    }
                } else {
                    mapResult.put("content", null);
                    mapResult.put("extra", null);
                    mapResult.put("message", "格式错误，多联格式：(孔数*跨径)+(孔数*跨径)...");
                    mapResult.put("status", "ERROR");
                }
            }
        } catch (Exception e) {
            mapResult.put("content", null);
            mapResult.put("extra", null);
            mapResult.put("message", "格式错误，多联格式：(孔数*跨径)+(孔数*跨径)...");
            mapResult.put("status", "ERROR");
        }
        return mapResult;
    }

    @PostMapping(value = "/bridge/bridgeSide/bridgeParts/bridgeSite/addSite")
    public Map<String, Object> addSite(@RequestBody Map<String, Object> requestMap) {
        int bridgeId = (int) requestMap.get("bridgeId");
        int sideTypeId = (int) requestMap.get("sideTypeId");
        int id = (int) requestMap.get("id");
        String bridgeSpanCombination = (String) requestMap.get("bridgeSpanCombination");
        List<Map<String, Object>> mapLst = new ArrayList<>();
        String[] arr = bridgeSpanCombination.split("\\+");
        int siteNo = 1;
        for (int i = 0; i < arr.length; i++) {
            String a = arr[i].substring(1, arr[i].length() - 1);
            int ks = Integer.valueOf(a.split("\\*")[0]);
            String span = a.split("\\*")[1];
            for (int j = 0; j < ks; j++) {
                Map<String, Object> map = new HashMap<>();
                map.put("jointNo", i + 1);
                map.put("span", span);
                map.put("bridgeId", bridgeId);
                map.put("sideTypeId", sideTypeId);
                map.put("siteNo", siteNo);
                map.put("siteName", "第" + siteNo + "孔");
                map.put("framingInfoId", id);
                mapLst.add(map);
                siteNo++;
            }
        }
        int code = frameMemberService.addSite(mapLst, bridgeSpanCombination, id, bridgeId, sideTypeId);
        Map<String, Object> mapResult = new HashMap<>();
        ResultResUtils.ResultRes(code, mapResult);
        return mapResult;
    }

    @PostMapping(value = "/bridge/bridgeSide/bridgeParts/addPier")
    public Map<String, Object> addPier(@RequestBody Map<String, Object> requestMap) {
        List siteNoList = (List) NumberUtils.getMiddleNumbersByTwoNumbers((int) requestMap.get("startSiteNo"), (int) requestMap.get("endSiteNo"));
        int code = frameMemberService.addPier(siteNoList, requestMap);
        Map<String, Object> map = new HashMap<>();
        ResultResUtils.ResultRes(code, map);
        return map;
    }

    @PostMapping(value = "/bridge/bridgeSide/bridgeParts/addSiteRange")
    public Map<String, Object> addSiteRange(@RequestBody Map<String, Object> requestMap) {
        List siteNoList = (List) NumberUtils.getMiddleNumbersByTwoNumbers((int) requestMap.get("startSiteNo"), (int) requestMap.get("endSiteNo"));
        int code = frameMemberService.addSiteRange(siteNoList, requestMap);
        return null;
    }

    @PostMapping(value = "/bridge/bridgeSide/bridgeParts/addSuperstructure")
    public Map<String, Object> addSuperstructure(@RequestBody Map<String, Object> requestMap) {
        List siteNoList = (List) NumberUtils.getMiddleNumbersByTwoNumbers((int) requestMap.get("startSiteNo"), (int) requestMap.get("endSiteNo"));
        int code = frameMemberService.addSuperstructure(siteNoList, requestMap);
        Map<String, Object> map = new HashMap<>();
        ResultResUtils.ResultRes(code, map);
        return map;
    }

    @GetMapping(value = "/bridge/bridgeSide/bridgeParts/getListByPier")
    public List<Map<String, Object>> getListByPier() {
        List<Map<String, Object>> listMap = frameMemberService.getListByPier();
        return listMap;
    }

    @GetMapping(value = "/bridge/bridgeSide/bridgeParts/getListByRiverbed")
    public List<Map<String, Object>> getListByRiverbed() {
        List<Map<String, Object>> listMap = frameMemberService.getListByRiverbed();
        return listMap;
    }

    @GetMapping(value = "/bridge/bridgeSide/bridgeParts/getListByRegulatingStructure")
    public List<Map<String, Object>> getListByRegulatingStructure() {
        List<Map<String, Object>> listMap = frameMemberService.getListByRegulatingStructure();
        return listMap;
    }

    @GetMapping(value = "/bridge/bridgeSide/bridgeParts/getListByDeck")
    public List<Map<String, Object>> getListByDeck() {
        List<Map<String, Object>> listMap = frameMemberService.getListByDeck();
        return listMap;
    }

    @GetMapping(value = "/bridge/bridgeSide/bridgeParts/getListByExpansionJoint")
    public List<Map<String, Object>> getListByExpansionJoint() {
        List<Map<String, Object>> listMap = frameMemberService.getListByExpansionJoint();
        return listMap;
    }

    @GetMapping(value = "/bridge/bridgeSide/bridgeParts/getListBySuperstructure")
    public List<Map<String, Object>> getListBySuperstructure(HttpServletRequest request) {
        int memberDescId = Integer.valueOf(request.getParameter("superstructureTypeId"));
        List<Map<String, Object>> listMap = frameMemberService.getListBySuperstructure(memberDescId);
        return listMap;
    }

    @GetMapping(value = "/bridge/bridgeSide/bridgeParts/getListByAbutment")
    public List<Map<String, Object>> getListByAbutment(HttpServletRequest request) {
        int memberDescId = Integer.valueOf(request.getParameter("abutmentTypeId"));
        List<Map<String, Object>> listMap = frameMemberService.getListByAbutment(memberDescId);
        return listMap;
    }

    @PostMapping(value = "/bridge/bridgeSide/bridgeParts/addAbutment")
    public Map<String, Object> addAbutment(@RequestBody Map<String, Object> requestMap) {
        List siteNoList = new ArrayList();
        if (0 < (int) requestMap.get("endSiteNo")) {
            siteNoList.add(1);
            siteNoList.add((int) requestMap.get("endSiteNo"));
        }
        int code = frameMemberService.addAbutment(siteNoList, requestMap);
        Map<String, Object> map = new HashMap<>();
        ResultResUtils.ResultRes(code, map);
        return map;
    }

    @PostMapping(value = "/bridge/bridgeSide/bridgeParts/addDeck")
    public Map<String, Object> addDeck(@RequestBody Map<String, Object> requestMap) {
        List siteNoList = NumberUtils.getSiteNoListUtil(requestMap);
        int code = frameMemberService.addDeck(siteNoList, requestMap);
        Map<String, Object> map = new HashMap<>();
        ResultResUtils.ResultRes(code, map);
        return map;
    }

    @PostMapping(value = "/bridge/bridgeSide/bridgeParts/addExpansionJoint")
    public Map<String, Object> addExpansionJoint(@RequestBody Map<String, Object> requestMap) {
        List siteNoList = NumberUtils.getSiteNoListUtil(requestMap);
        int code = frameMemberService.addExpansionJoint(siteNoList, requestMap);
        Map<String, Object> map = new HashMap<>();
        ResultResUtils.ResultRes(code, map);
        return map;
    }


    @DeleteMapping(value = "/bridge/bridgeSide/bridgeParts/delete")
    public Map<String, Object> delete(@RequestBody Map<String, Object> requestMap) {
        int id = (int) requestMap.get("id");
        int code = frameMemberService.deleteStructInfoById(id);
        Map<String, Object> map = new HashMap<>();
        ResultResUtils.ResultRes(code, map);
        return map;
    }

    @PostMapping(value = "/bridge/bridgeSide/bridgeParts/edit")
    public Map<String, Object> edit(@RequestBody Map<String, Object> requestMap) {
        int code = frameMemberService.edit(requestMap);
        Map<String, Object> map = new HashMap<>();
        ResultResUtils.ResultRes(code, map);
        return map;
    }

}